//
//  YMSComposeView.swift
//  YMSWeibo
//
//  Created by 杨茂盛 on 2016/11/20.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import pop
import SnapKit
class YMSComposeView: UIView {
    //定义一个数组来存放按钮
    lazy var myBtnArray:[YMSComposeButton] = [YMSComposeButton]()
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        //添加毛玻璃效果
        let backView = UIImageView(image: UIImage.snapShotCurrentWindow().applyExtraLightEffect())
        //let toolBar = UIToolbar(frame: UIScreen.main.bounds)
        //toolBar.barStyle = .default
        self.addSubview(backView)
        //添加顶部文字
        self.addSubview(slogonView)
        
        //约束顶部文字
        slogonView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        //添加撰写按钮
        addWriteBtn()
        
    }
    //MARK: 在该view被添加到其它视图的时候,就开始做动画
    override func willMove(toSuperview newSuperview: UIView?) {
        for (index,btn) in myBtnArray.enumerated() {
            self.writeBtnStartAnimation(index: index, btn: btn)  //在view被添加到其它视图上的时候,即开始执行动画,默认ishow是true
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       // backView.removeFromSuperview()
        
        for (index,btn) in myBtnArray.reversed().enumerated() {
            //当点击touchBegan的方法的时候,ishow = false 这个时候 isShow ? -350:350  y就为350 就回到屏幕下方了
            self.writeBtnStartAnimation(index: index, btn: btn, isShow: false)
        }
        //延迟
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
            //移除动画
            self.removeFromSuperview()
        })
    }
    //MARK: 添加撰写按钮
    func  addWriteBtn(){
        //按钮的宽和高
        let btnWidth:CGFloat = 80
        let btnHeight:CGFloat = 110
        //按钮之间的间距
        let btnMargin = (ScreenWidth - 3*btnWidth) / 4
        for i in 0..<6 {
            let myBtn = YMSComposeButton()
            //            myBtn.tag = i
            //确定按钮的行和列
            let colCount = i % 3
            let rowCount = i / 3
            //确定按钮的frame x
            let btnX = btnMargin + (btnWidth + btnMargin)*CGFloat(colCount)
            let btnY = (btnHeight + btnMargin) * CGFloat(rowCount) + ScreenHeight //开始让按钮在下面 不显示在屏幕上
            myBtn.frame = CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)

            //给按钮添加图片
            myBtn.setImage(#imageLiteral(resourceName: "tabbar_compose_friend"), for: .normal)
            //设置文字
            myBtn.setTitle("呵呵哒", for: .normal)
            self.addSubview(myBtn)
            myBtnArray.append(myBtn)
            myBtn.addTarget(self, action: #selector(clickWriteBtnScale(btn:)), for: .touchUpInside)
        }
    }
    //MARK: 按钮的监听方法,点击当前的按钮变大,其它的按钮变小
    @objc private func clickWriteBtnScale(btn:YMSComposeButton){
        
        UIView.animate(withDuration: 0.5, animations: {
            //遍历数组获取被点击的按钮
            for composeBtn in self.myBtnArray{
                if btn == composeBtn {
                    //拿到了当前被点击了的按钮
                    composeBtn.transform = CGAffineTransform.init(scaleX: 1.7, y: 1.7)
                }else{
                composeBtn.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
                    composeBtn.alpha = 0.1
                }
            }
        }, completion: { (_) in
            //跳转控制器
            //应该动态的获取控制器的名称
            let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
            
            let composeVcString = bundleName + "." + "YMSComposeController" //命名空间的缘故
            //获取类
            guard let classType = NSClassFromString(composeVcString) as? YMSComposeController.Type else{
                return
            }
            let Vc = classType.init()
            let Nav = UINavigationController(rootViewController: Vc)
//            Nav.view.backgroundColor = #colorLiteral(red: 0.9411764741, green: 0.4980392158, blue: 0.3529411852, alpha: 1)
            UIApplication.shared.keyWindow?.rootViewController?.present(Nav, animated: true, completion:{
                self.removeFromSuperview()//要销毁当前视图
            })
        })
    }
    
    //MARK: 按钮的动画
    func writeBtnStartAnimation(index:Int,btn:YMSComposeButton,isShow:Bool = true){
        //添加动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置属性
        springAnimation?.springBounciness = 10.0
        springAnimation?.springSpeed = 10
        //设置延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index)*0.025
        let btnCentY = btn.center.y + CGFloat(isShow ? -350:350) //刚开始处于屏幕下方,点击撰写按钮的时候,isShow == true frame.orgin.y 就为-350  向上偏移350
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCentY))
        btn.pop_add(springAnimation, forKey: nil)
        
    }
    //顶部的文字
    private lazy var slogonView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "compose_slogan"))

    
}
